package com.akuhome.resource.service;

import com.akuhome.resource.DO.Approve;
import com.akuhome.resource.dao.ApproveDao;
import com.akuhome.resource.entity.Project;
import com.akuhome.resource.entity.User;
import com.akuhome.resource.enums.NeedStatus;
import com.akuhome.resource.enums.NeedType;
import com.akuhome.resource.enums.ProjectStatus;
import com.akuhome.resource.enums.ResultEnum;
import com.akuhome.resource.exception.ResourceException;
import com.akuhome.resource.service.entity.ProjectService;
import com.akuhome.resource.service.entity.UserService;
import com.akuhome.resource.service.need.AccNeedService;
import com.akuhome.resource.service.need.ClassroomNeedService;
import com.akuhome.resource.service.need.FoodNeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApproveService {
    @Autowired
    private ApproveDao approveDao;
    @Autowired
    private UserService userService;
    @Autowired
    private AccNeedService accNeedService;
    @Autowired
    private ClassroomNeedService classroomNeedService;
    @Autowired
    private FoodNeedService foodNeedService;
    @Autowired
    private ProjectService projectService;

    private static final List<Approve> simpleList = new ArrayList<>();

    static {
        simpleList.add(new Approve(NeedType.ACC_NEED, "住宿需求"));
        simpleList.add(new Approve(NeedType.CLASSROOM_NEED,"教室需求"));
        simpleList.add(new Approve(NeedType.FOOD_NEED,"用餐需求"));
    }

    public List<Approve> findApproveByProjectUId(String uid){
        return approveDao.findApproveByProjectUid(uid);
    }

    public Approve findApproveByProjectUIdAndType(String uid,NeedType approveType){
        return approveDao.findApproveByProjectUidAndApproveType(uid,approveType);
    }

    public void initApproveNode(String projectUid){
        List<Approve> approveList = new ArrayList<>(simpleList);
        approveList.forEach((value)->{
            value.setProjectUid(projectUid);
        });
        insertApproves(approveList);
    }

    public void insertApprove(Approve approve){
        approveDao.insertApprove(approve);
    }

    public void insertApproves(List<Approve> approveList){
        approveDao.insertApproves(approveList);
    }

    public void updateApprove(Approve approve){
        approveDao.updateApprove(approve);
    }

    public Boolean checkApproveAllPass(String uid){
        boolean result = true;
        List<Approve> approveList = approveDao.findApproveByProjectUid(uid);
        for(Approve approve : approveList){
            result = approve.getResult() != null && (result ? approve.getResult() : false);
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public void submitApprove(Approve approve){
        approve.setDate(new Date());
        User user = new User();
        user.setId(userService.getCurrentUserId());
        approve.setUser(user);
        approveDao.updateApprove(approve);
        if(approve.getResult() != null)
            this.updateNeedStatus(approve.getNeedType(),approve.getProjectUid(),approve.getResult());
    }

    private void updateNeedStatus(NeedType needType,String uid,Boolean pass){
        NeedStatus status;
        if(pass){
            status = NeedStatus.COMPLETE;
        }else {
            status = NeedStatus.WAIT_MODIFY;
        }
        switch (needType){
            case ACC_NEED:
                accNeedService.updateNeedStatusByProjectUid(uid,status);
                break;
            case CLASSROOM_NEED:
                classroomNeedService.updateNeedStatusByProjectUid(uid,status);
                break;
            case FOOD_NEED:
                foodNeedService.updateNeedStatusByProjectUid(uid,status);
                break;
            default:
                throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
        updateProjectStatus(uid,pass);
    }

    private void updateProjectStatus(String uid,Boolean pass){
        if(pass){
            if(checkApproveAllPass(uid)){
                projectService.updateProjectStatus(uid, ProjectStatus.WAIT_CONFIRM);
            }
        }else{
            projectService.updateProjectStatus(uid,ProjectStatus.WAIT_MODIFY);
        }
    }
}
